﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace Tencent.OA.App
{
    public sealed class TencentConfigurationSectionGroup : ConfigurationSectionGroup
    {
        /// <summary>
        /// <para> 定义一个 TencentConfigurationSectionGroup 配置节组的节点名。</para>
        /// </summary>
        public static readonly string TencentConfigurationSectionGroupName = "tencent.OA";

        private static TencentConfigurationSectionGroup m_instance;
        public static TencentConfigurationSectionGroup GetSectionGroup()
        {
            if (m_instance == null)
            {
                Configuration config = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap()
                                        {
                                            ExeConfigFilename = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
                                        },
                                        ConfigurationUserLevel.None);
                m_instance = config.SectionGroups[TencentConfigurationSectionGroupName] as TencentConfigurationSectionGroup;
            }
            return m_instance;
        }

        public static T GetSection<T>(string sectionName) where T : class
        {
            if (sectionName == null)
                throw new ArgumentNullException("sectionName");

            T section = ConfigurationManager.GetSection(TencentConfigurationSectionGroupName + "/" + sectionName) as T;
            if (section == null)
                throw new ConfigurationErrorsException("必须在配置组 '" + TencentConfigurationSectionGroupName + "' 中指定配置节 '" + sectionName + "'。");
            return section;
        }

        [ConfigurationProperty(ScheduleServiceSection.ScheduleServiceSectionKey)]
        public ScheduleServiceSection Schedules
        {
            get
            {
                return (ScheduleServiceSection)base.Sections[ScheduleServiceSection.ScheduleServiceSectionKey];
            }
        }
        /// <summary>
        /// 日志节点配置。
        /// </summary>
        [ConfigurationProperty(LoggerSection.LoggerSectionKey)]
        public LoggerSection Logger
        {
            get
            {
                return (LoggerSection)base.Sections[LoggerSection.LoggerSectionKey];
            }
        }

    }

    /// <summary>
    /// 程序集信息
    /// </summary>
    public class AssemblyInfo
    {
        /// <summary>
        /// 获取Assembly的运行路径
        /// </summary>
        /// <returns></returns>
        public static string GetAssemblyPath()
        {
            string _CodeBase = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;

            _CodeBase = _CodeBase.Substring(8, _CodeBase.Length - 8);    // 8是 file:// 的长度

            string[] arrSection = _CodeBase.Split(new char[] { '/' });

            string _FolderPath = "";
            for (int i = 0; i < arrSection.Length - 1; i++)
            {
                _FolderPath += arrSection[i] + "/";
            }

            return _FolderPath;
        }
    }
}
